package com.example.leetcode.linkedlist;

import com.example.leetcode.linkedlist.common.LinkedListUtils;
import com.example.leetcode.linkedlist.common.ListNode;

import java.util.List;

/**
 * Definition for singly-linked list.
 * public class ListNode {
 * int val;
 * ListNode next;
 * ListNode(int x) {
 * val = x;
 * next = null;
 * }
 * }
 */
class Solution160 {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        return LinkedListUtils.getIntersectionNode(headA, headB);
    }
}

public class leetcode160 {
    public static void main(String[] args) {
        Solution160 solution160 = new Solution160();

        int[] nums1 = {3};
        int[] nums2 = {2,3};
        List<ListNode> heads = getHeads(3, nums1, nums2, 0, 1);
        test160(solution160, heads);

        int[] nums3 = {4, 1, 8, 4, 5};
        int[] nums4 = {5, 6, 1, 8, 4, 5};
        List<ListNode> heads1 = getHeads(8, nums3, nums4, 2, 3);
        test160(solution160, heads1);

        int[] nums5 = {2,6,4};
        int[] nums6 = {1,5};
        List<ListNode> heads2 = getHeads(0, nums5, nums6, 3, 2);
        test160(solution160, heads2);
    }

    private static void test160(Solution160 solution160, List<ListNode> heads1) {
        ListNode result1 = solution160.getIntersectionNode(heads1.get(0), heads1.get(1));
        System.out.println(result1 == null ? null : result1.val);
    }

    private static List<ListNode> getHeads(int intersectVal, int[] listA, int[] listB, int skipA, int skipB) {
        List<ListNode> heads = LinkedListUtils.generateLinkedListWithIntersectionNode(intersectVal, listA, listB, skipA, skipB);
        System.out.println(LinkedListUtils.getLinkedListVals(heads.get(0)));
        System.out.println(LinkedListUtils.getLinkedListVals(heads.get(1)));
        return heads;
    }
}
